<template>
  <base-breadcrumb>
    <a-form-model :model="form" ref="ruleForm"
                          :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                          :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"  >
      <a-card :body-style="{ padding: '24px 32px' }" :bordered="false">
        <div>
            <title-name title="考察信息" ></title-name>
              <!-- 服务 -->
              <service
                  @submitNext="getsubmitNext" 
                  ref="serverRef" 
                  :serviceList ="serviceList" 
                  v-if="( investigateType =='服务类分包商考察')"
                  @validChanged="handleValidChanged"></service>
              <!-- 甲指 -->
              <Fingernail
                  @submitNextSteps="getsubmitNextSteps"  
                  ref="serverRef" 
                  :fingernailList="fingernailList" 
                  v-if="(   investigateType =='甲指分包商考察') "
                  @validChanged="handleValidChanged" 
                  ></Fingernail>
              <!-- 劳务 -->
              <labour
               @submitNextStep="getsubmitNextStep"  
               ref="serverRef" :labourList="labourList" 
               v-if="( investigateType =='劳务/专业分包商考察')"
               @validChanged="handleValidChanged"></labour>
        </div>
      </a-card>
    </a-form-model>
        <div style="float:right" class="page-btn-right-top">
          <a-button  class="button ml12" @click="preSave" type="primary">提交</a-button>
          <!--<a-button  class="button ml12" type="primary" @click="goBack">关闭</a-button>-->
        </div>
  </base-breadcrumb>
</template>


<script>
  import { getAttachment } from '@/api/commons'
  import { STable, Ellipsis } from '@/components'
  import {
    basicInformation,
    getGroup,
    addbasicInformation,
    editbasicInformation,
    getbasicInformation ,
    addLabourBasic,
    updateSubStatus,
    editfingernailList, investigateCheck } from '@/api/subcontractors/subcontractors'
  import Service from '@/views/subcontractors/investigate/components/Service'
  import Fingernail from '@/views/subcontractors/investigate/components/Fingernail'
  import Labour from '@/views/subcontractors/investigate/components/Labour'
  import {getOrgUsers ,getUsersDetail,getEmployeesList} from '@/api/commons';
  import {getlabourService  ,editlabourList} from '@/api/subcontractors/subcontractors'
  import {addfingernailList , getfingernailList,getServiceInformation ,deleteSub ,istheSameInvestigate} from '@/api/subcontractors/subcontractors'
  import { addInformatinService, editInformatinService} from '@/api/subcontractors/subcontractors'
  import { getLaborSubcontractors , getStatusCodeData} from '@/api/subcontractors/labor'
  import { addGroup ,editGroup, deleteGroup , getInvestigateMember,addMemberListsArr,deleteMemberListsArr} from '@/api/subcontractors/subcontractors'
  import { createValidTool } from '@/utils/regExpression'


  // 数组创建时候的下标
  var numbe = 0

  export default {
  name:'a'+Date.now(),
    components: {
      Service,
      Fingernail,
      Labour,
    },
    props: {
      text: String
    },
    watch: {
      isAudit: {
        immediate: true,
        handler(val) {
            if (val) {
                this.type = 'view'
            }
        }
      }
    },

    data() {
      return {
        isValid:0, // 是否验证
        loadingState: {
          isRecomd: false,
          isQuality: false
        },
        total:'',
        qulSurveyId: '',//从合格分包商选择的带出来的考察id
        visible: false,
        qulVisible:false,
        isSubmitOrSave:'save',
        loading:false,
        loadings:false,
        nextSteps: '1',
        preStep:1,
        istheSameInvestigateType:false,//是否提交成功
        childValidInfo: createValidTool(), // 验证子组件信息
        searchQueryQul:{
          pageNo:1,
          pageSize:10,
        },

        editTargetIndex: null, // 考察小组人员，编辑目标在表格中的序号
        searchQuery:{
          pageNo:1,
          pageSize:10,
        },
        subcontractorName:'',
        disabled:false,
        subcontractorId:"",
        selectDatas: {}, //
        isSaveDraft:false,
        formAdd:{},
        list:[],
        editingKey: '',
        // 附件上传
        listUrl:'/cscec-project-business/subcontractors-file/inspect/query',
        deleteUrl:'/cscec-project-business/subcontractors-file/inspect/delete',
        uploadList:'/cscec-project-business/subcontractors-file/inspect/upload',
        reqMethod:'post',
        businessId:'',
        title:'',
        labourList:{},
        pageSize:10,
        pageNumber:1,
        visibleGroup:false,
        fileList: [
          {
            uid: '-1',
            name: 'image.png',
            status: 'done',
            url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
          }
        ],
        headers: {
          authorization: 'authorization-text'
        },
        form:{ // 表单数据
          investigateType:'',
          list:[],
          subcontractorsType: []
        },
        status: 'all',
        employee:'',
        areaCode: '',
        basicList:{},
        titles:'',
        qultotal: 0,

        qultotalPage: null, // 总条数
        remtotal: 0,

        remtotalPage: null, // 总条数
        dataList:[],
        ifReplace: null, // 是否替换小组成员
        type:false,
        groupLists:[],
        memberList:[],
        serviceList:{},
        fingernailList:{},
        id:'',
        subId:'', //分包商id
        designTypes:[ ],
        isCompany:false,
        isManager:false,
        selectedRows:[],
        selectedRowKeys: [],
        qulRecomSubselectedRowKeys:[],
        qulRecomSubselectedRows:[],
        RecomSubselectedRows:[],
        RecomSubselectedRowKeys: [],
        recommeddList:[],
        invesList:[],
        addInvestigateId:'',
        Modifyfield: {
          children: 'children',
          title: 'name',
          id: 'id',
        },
        uuid:'',   // 链接带的uuid
        surveyId:'',  // 分包商考察id
        investigateType:'',  // 考察类型
      }
    },
    created() {
      this.addInvestigateId = JSON.parse(sessionStorage.getItem("addInvestigateId"))
      this.type = this.$route.query.type

      this.id = this.$route.query.id || this.addInvestigateId
      this.businessId = this.$route.query.id || this.id

      this.investigateCheckReq()


    },
    destroyed(){
      let addInvestigateId =  JSON.parse(sessionStorage.getItem("addInvestigateId"))
      if(addInvestigateId){
        sessionStorage.removeItem("addInvestigateId")
      }
    },
    computed: {
      rowSelection() {
        return {
          selectedRowKeys: this.selectedRowKeys,
          selectedRows: this.selectedRows,   // 当前行所有数据
          onChange: this.onSelectChange
        }
      },
    },
    methods: {
      investigateCheckReq(){
        let that = this;
        investigateCheck({uuid:that.id}).then(res=>{
          if(res.code == 200 || res.data.code == 200){
            that.surveyId = res.data.surveyId
            console.log("fhdsafjas:",res.data.investigateType)
            that.investigateType = res.data.investigateType
            sessionStorage.setItem("addInvestigateId",JSON.stringify(res.data.surveyId))

            this.getbasicInformation()

            if(res.data.investigateType == '服务类分包商考察'){
            // 服务
              getServiceInformation({id:res.data.surveyId}).then((res1)=>{
                that.serviceList = res1.data
              })
            }else if(res.data.investigateType == '甲指分包商考察'){
              // 甲指
              getfingernailList({id:res.data.surveyId}).then((res2)=>{
                that.fingernailList = res2.data
              })
            }else if(res.data.investigateType == '劳务/专业分包商考察'){
              // 劳务
              getlabourService({id:res.data.surveyId}).then((res3)=>{
                that.labourList = res3.data
              })
            }
          }
        })
      },

    handleValidChanged(data) {
      this.childValidInfo.update(data)
    },

      // 保存考察信息草稿
      getsubmitNext(val){
        if(this.type!='view'){
          // 验证子组件是否有内容填写错误
          const validInfo = this.childValidInfo.validate()
          if(!validInfo.valid) {
            this.$message.warn(validInfo.message)
            this.nextSteps = '2'
            return false
          }
          this.nextSteps='3'
          this.form.surveyId = this.id
          this.serviceList.investigateTypeCode = '1'
          // 当没有小组长时，不访问接口
          let groupLeader=this.memberList.filter(item=>item.type=='1')
          if(groupLeader.length!==0){
            editInformatinService(this.serviceList).then(res=>{
              if(res.code==200 || res.data.code == 200){
                this.businessId = res.data.investigateId
                this.getbasicInformation()
                this.$message.success("提交成功")
              }
            })
          }
        }else{
          this.nextSteps='3'
        }
      },
      // 甲指
      getsubmitNextSteps(val){
        if(this.type!='view'){
          // 验证子组件是否有内容填写错误
          const validInfo = this.childValidInfo.validate()
          if(!validInfo.valid) {
            this.$message.warn(validInfo.message)
            this.nextSteps = '2'
            return false
          }
          this.nextSteps='3'
          this.fingernailList.investigateTypeCode = '2'
          editfingernailList(this.fingernailList).then(res=>{
            if(res.code==200 || res.data.code == 200){
              this.businessId = res.data.investigateId
              this.getbasicInformation()
              this.istheSameInvestigateType=true
              this.$message.success("提交成功")
            }
          })
        }else{
          this.nextSteps='3'
        }
      },
      // 劳务分包
      getsubmitNextStep(val){
          // 验证子组件是否有内容填写错误
          const validInfo = this.childValidInfo.validate()
          if(!validInfo.valid) {
            this.$message.warn(validInfo.message)
            this.nextSteps = '2'
            return false
          }
        this.nextSteps='3'
        this.labourList.investigateId = this.id
        this.labourList.investigateTypeCode = '3'
        this.labourList.lsebId = this.labourList.subcontractorBase.evaluationId
        if(this.type!='view'){
          editlabourList(this.labourList).then(res=>{
            if(res.code==200 || res.data.code == 200){
              this.businessId = res.data.investigateId
              this.getbasicInformation()
              this.istheSameInvestigateType=true
              this.$message.success("提交成功")
            }
          })
        }
      },


      // 保存草稿
      preSave(){
      // 验证子组件是否有内容填写错误
      const validInfo = this.childValidInfo.validate()
      if(!validInfo.valid) {
        this.$message.warn(validInfo.message)
        return false
      }
        this.isSaveDraft = true
        this.submit()
      },
      // 提交草稿
      submit(){
        let that = this;


        // 考察信息
          let result = that.$refs.serverRef.serviceList || that.$refs.serverRef.fingernailList ||that.
            $refs.serverRef.labourList || []

            investigateCheck({uuid:that.id}).then(resquest=>{
              if(resquest.code == 200 || resquest.data.code == 200){
                if(that.investigateType =='服务类分包商考察'){
                  result.investigateId = that.surveyId
                  result.investigateTypeCode = '1'
                  editInformatinService(result).then(res=>{
                    that.loading = false
                    if(res.code==200 || res.data.code==200){
                      // 防止保存草稿时，页面展示异常
                      that.getbasicInformation()
                      that.istheSameInvestigateType=true
                      if(that.isSubmitOrSave=='save'){
                        that.$message.success("提交成功")
                      }
                    }
                  })
                }else if(that.investigateType =='甲指分包商考察'){
                  result.investigateId = that.surveyId
                  result.investigateTypeCode = '2'
                  editfingernailList(result).then(res=>{
                    that.loading = false
                    if(res.code==200 || res.data.code==200){
                      that.getbasicInformation()
                      that.istheSameInvestigateType=true
                      if(that.isSubmitOrSave=='save' ){
                        that.$message.success("提交成功")
                      }
                    }
                  })
                }else if(that.investigateType=='劳务/专业分包商考察'){
                  result.investigateId = that.surveyId
                  result.investigateTypeCode = '3'
                  editlabourList(result).then(res=>{
                    that.loading = false
                    if(res.code==200 || res.data.code==200){
                      that.getbasicInformation()
                      that.istheSameInvestigateType=true
                      if(that.isSubmitOrSave=='save'){
                        that.$message.success("提交成功")
                      }
                    }
                  })
                }
              }else{
                that.$message.error("链接已过期需要再次分享后录入信息")
              }
            })

      },


      // 查询第一个tab页的详情
      getbasicInformation(){
        if(this.id){
          getbasicInformation({id:this.surveyId}).then((res)=>{
            this.form = res.data
            const list = []
            this.form.typeList && this.form.typeList.forEach(item=>{
              list.push(item.name)
            })
            //fix- select placeholader未展示bug
            if(!this.form.investigateType){
              this.form.investigateType = undefined
            }
            if(!this.form.branchCompanyId){
              this.form.branchCompanyId = undefined
            }
            this.form = {
              ...this.form,
              subcontractorsType: list
            }
            this.memberList = this.form.memberList
          })
        }
      },


      // goBack(){
      //   window.close()
      // },

      onSelectChange(selectedRowKeys,selectedRows) {
        this.selectedRowKeys = selectedRowKeys;
        this.selectedRows = selectedRows
      },
    }
  }
</script>

<style lang="less" scoped>
  .wrapper-row {
    padding-left: 55px;
    font-weight: bold;
    letter-spacing: 1px;
  }

  /deep/ .ant-form-item label {
    font-size: 12px;
  }

  /deep/ .ant-btn {
    border-radius: 5px;
  }

  // /deep/ .ant-calendar-picker-input.ant-input {
  //   width: 100%;
  // }

  /deep/ .ant-input {
    /*width: 70%;*/
    border-radius: 5px;
  }

  // /deep/ .ant-calendar-picker {
  //   width: 100%;
  //   border-radius: 5px;
  // }

  /deep/ .ant-card.ant-card-bordered {
    border-radius: 5px;
  }

  .ant-avatar-lg {
    width: 48px;
    height: 48px;
    line-height: 48px;
  }

  .list-content-item {
    color: rgba(0, 0, 0, 0.45);
    display: inline-block;
    vertical-align: middle;
    font-size: 14px;
    margin-left: 40px;
    span {
      line-height: 20px;
    }
    p {
      margin-top: 4px;
      margin-bottom: 0;
      line-height: 22px;
    }
  }

  .title-name {
    border-left: 5px solid #1890ff;
    font-weight: bold;
    font-size: 16px;
    margin-bottom: 20px;
    padding-left: 15px;
    height: 20px;
    line-height: 20px;
  }

  /deep/ .table-page-search-wrapper .ant-form-inline .ant-row .ant-col.ant-col-sm-24.ant-col-md-12 {
    padding-left: 80px;
  }
  /deep/ .ant-card-bordered{
    margin-top:60px
  }
  /deep/ .ant-table-pagination.ant-pagination{
    display:none
  }
</style>

